﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using ClientManager.Data;
using ClientManager.Data.Infrastucture;
using ClientManager.Model;
using ClientManager.Service.Clients;
using ClientManager.Service.ServiceLevels;
using ClientManager.Web.Models;

namespace ClientManager.Web.Controllers
{
    public class ClientController : Controller
    {
        private readonly IClientService _clientService;
        private readonly IServiceLevelService _serviceLevelService;

        public ClientController(IClientService clientService, IServiceLevelService serviceLevelService)
        {
            _clientService = clientService;
            _serviceLevelService = serviceLevelService;
        }

        // GET: Clients
        public ActionResult Index()
        {
            var clients = Mapper.Map<IList<ClientViewModel>>(_clientService.GetAll(x => x.ServiceLevel));
            return View(clients);
        }

        // GET: Clients/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var client = _clientService.GetById(id.Value, x=>x.ServiceLevel);

            if (client == null)
            {
                return HttpNotFound();
            }

            var clientVm = Mapper.Map<ClientViewModel>(client);
            return View(clientVm);
        }

        // GET: Clients/Create
        public ActionResult Create()
        {
            var clientVm = new ClientViewModel();
            var serviceLevels =  Mapper.Map<IList<ServiceLevelViewModel>>(_serviceLevelService.GetAll());
            clientVm.ServiceLevels = new SelectList(serviceLevels, "ServiceLevelId", "Name");
            return View(clientVm);
        }

        // POST: Clients/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ClientId,FirstName,Surname,DateCreated,ServiceLevelId")] ClientViewModel clientViewModel)
        {
            if (ModelState.IsValid)
            {
                clientViewModel.DateCreated = DateTime.Now;
                var client = Mapper.Map<Client>(clientViewModel);
                _clientService.Create(client);
                return RedirectToAction("Index");
            }

            var serviceLevels = Mapper.Map<IList<ServiceLevelViewModel>>(_serviceLevelService.GetAll());
            clientViewModel.ServiceLevels = new SelectList(serviceLevels, "ServiceLevelId", "Name");
            return View("Create",clientViewModel);
        }

        // GET: Clients/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var client = _clientService.GetById(id.Value, x => x.ServiceLevel);
            if (client == null)
            {
                return HttpNotFound();
            }

            var clientViewModel = Mapper.Map<ClientViewModel>(client);
            var serviceLevels = Mapper.Map<IList<ServiceLevelViewModel>>(_serviceLevelService.GetAll());
            clientViewModel.ServiceLevels = new SelectList(serviceLevels, "ServiceLevelId", "Name");
            return View(clientViewModel);
        }

        // POST: Clients/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ClientId,FirstName,Surname,DateCreated,ServiceLevelId")] ClientViewModel clientViewModel)
        {
            if (ModelState.IsValid)
            {
                var client = Mapper.Map<Client>(clientViewModel);
                _clientService.Update(client);
                return RedirectToAction("Index");
            }

            var serviceLevels = Mapper.Map<IList<ServiceLevelViewModel>>(_serviceLevelService.GetAll());
            clientViewModel.ServiceLevels = new SelectList(serviceLevels, "ServiceLevelId", "Name");
            return View("Edit",clientViewModel);
        }

        // GET: Clients/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var client = _clientService.GetById(id.Value, x=> x.ServiceLevel);
            if (client == null)
            {
                return HttpNotFound();
            }
            var clientVm = Mapper.Map<ClientViewModel>(client);

            return View(clientVm);
        }

        // POST: Clients/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var client = _clientService.GetById(id);
            _clientService.Delete(client);
            return RedirectToAction("Index");
        }

       
    }
}
